<script>
    import {
        baseUrlmember
    } from 'untils/auth.js'
    export default {
        onLaunch: function() {
            if (uni.getStorageSync('userInfo').token == undefined) {
                uni.reLaunch({
                    url: '/pages/login/login',
                });
            }
            let _this=this;
			console.log('App onLaunch')
            // #ifdef APP-PLUS
            plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
				console.log('widgetInfo:',widgetInfo)
                //当前版本号
                let appVersions = widgetInfo.versionCode;
                console.log('当前版本号' + appVersions)
                uni.request({
                    url: baseUrlmember() + '/user/info/checkAppVersion',
                    method: 'POST',
                    data: {
                        appVersion: appVersions,
                        clientType: 1 //客户端类型，1：android ,2：ios
                    },
                    success: (res) => {
                        console.log('最新版本号' + res.data.data.versionCode)
                        if (res.data.code == '200') {
                            //下载地址
                            let apkurl = res.data.data.dowoloadUrl;
                            //判断版本号,返回版本大于当前版本需要更新，
                            if (res.data.data.versionCode > appVersions) {
                                uni.getSystemInfo({
                                    success: (respon) => {
                                        //如果是安卓端
                                        if (respon.platform == "android") {
                                            //true为强制更新
                                            console.log(res.data.data.enableForcedUpdated)
                                            if (res.data.data.enableForcedUpdated == true) {

                                                uni.showModal({
                                                    content: res.data.data.tipsContent,
                                                    confirmText: '确定',
                                                    confirmColor: "#FEAE25",
                                                    showCancel: false,
                                                    success: function(res) {
                                                        if (res.confirm) {
                                                            console.log('用户点击确定');                                           
                                                            _this.updateAndroid(apkurl);
                                                        }
                                                    }
                                                });
                                            } else {
                                                uni.showModal({
                                                    content: res.data.data.tipsContent,
                                                    confirmText: '确定',
                                                    confirmColor: "#FEAE25",
                                                    cancelText: '取消',
                                                    success: function(res) {
                                                        if (res.confirm) {
                                                            console.log(
                                                                '用户点击确定-非强制更新'
                                                                );
                                                            _this.updateAndroid(apkurl);
                                                        } else if (res
                                                            .cancel) {
                                                            console.log(
                                                                '用户点击取消'
                                                                );
                                                        }
                                                    }
                                                });
                                            }
                                        } else if (respon.platform == "ios") {
                                            console.log('ios端')
                                        }
                                    }
                                })
                            }
                        }
                    },
                    fail: (e) => {
                        console.log(e)
                    }
                })
            })
            // #endif
        },
        methods: {
            //app更新,安卓端
            updateAndroid(downurl) {
                uni.showLoading({
                    title: '更新中……'
                });
                let _this = this;
                var dtask = plus.downloader
                    .createDownload(downurl, {},
                        function(d, status) {
                            console.log('1')
                            // 下载完成  
                            if (status == 200) {
                                console.log('2')
                                uni.hideLoading();
                                plus.runtime
                                    .install(plus.io
                                        .convertLocalFileSystemURL(
                                            d
                                            .filename
                                        ), {}, {},
                                        function(
                                            error) {
                                            uni.showToast({
                                                title: '安装失败',
                                                mask: false,
                                                duration: 1500
                                            });
                                        })
                            } else {
                                console.log('3')
                                uni.hideLoading();
                                uni.showToast({
                                    title: '更新失败',
                                    mask: false,
                                    duration: 1500
                                });
                            }
                        });
                dtask.start();
            }
        },
        onShow: function() {
            console.log('App Show')
        },
        onHide: function() {
            console.log('App Hide')
        }
    }
</script>

<style>
    .uni-picker-container {
        z-index: 10080;
    }

    /*每个页面公共css */
    checkbox .uni-checkbox-input {
        border-radius: 50%;
    }
</style>
